package cn.gbase.jiangsu.data.transfer.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.DaoException;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import cn.gbase.jiangsu.data.transfer.bean.ProductSortTree;
import cn.gbase.jiangsu.data.transfer.bean.ProductSortTreeNode;
import cn.gbase.jiangsu.data.transfer.common.SystemException;
import cn.gbase.jiangsu.data.transfer.service.ProductSortTreeNodeService;
import cn.gbase.jiangsu.data.transfer.service.ProductSortTreeService;

@IocBean
@At("/sortTree")
public class ProductSortTreeNodeAction {

	@Inject
	private ProductSortTreeService productSortTreeService;

	@Inject
	private ProductSortTreeNodeService productSortTreeNodeService;

	//@Inject
	//private StandardWithTreeNodeService standardWithTreeNodeService;

	@At("/listNode")
	@Ok("jsp:/sortTree/listNode")
	public QueryResult listNode(@Param("treeId") int treeId, @Param("p") int pageNumber, @Param("code") String code,
			@Param("name") String name, HttpServletRequest request) {

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("treeId", "" + treeId);
		if (treeId > 0) {
			ProductSortTree tree = productSortTreeService.getStandTree(treeId);
			if (tree != null) {
				itemMap.put("treeName", tree.getName());
			}
		} else {
			itemMap.put("treeName", "");
		}
		itemMap.put("code", code);
		itemMap.put("name", name);
		request.setAttribute("formdata", itemMap);

		// 查询数据
		return productSortTreeNodeService.getStandTreeNodeList(treeId, pageNumber, code, name);
	}

	@At("/listAllNode")
	@Ok("jsp:/sortTree/listAllNode")
	public List<ProductSortTreeNode> listAllNode(@Param("treeId") int treeId, HttpServletRequest request) {

		// 查询数据
		return productSortTreeNodeService.getStandTreeNodeList(treeId);
	}

	@At("/addNode")
	@Ok("jsp:/sortTree/addNode")
	public void toAddPage(@Param("treeId") int treeId, HttpServletRequest request, HttpSession session) {

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("treeId", "" + treeId);
		request.setAttribute("formdata", itemMap);

		// 回传表单输入
		Map<String, String> listMap = new HashMap<String, String>();
		listMap.put("pid", createTreeItems(treeId));
		session.setAttribute("listdata", listMap);
	}

	@At("/addNode")
	@POST
	@Ok(">>:/sortTree/listNode.nut?treeId=${obj.treeId}")
	@Fail("jsp:/sortTree/addNode")
	public ProductSortTreeNode add(@Param("..") ProductSortTreeNode node, HttpServletRequest request,
			HttpSession session) {
		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("treeId", "" + node.getTreeId());
		request.setAttribute("formdata", itemMap);

		// 回传表单输入
		Map<String, String> listMap = new HashMap<String, String>();
		listMap.put("pid", createTreeItems(node.getTreeId()));
		session.setAttribute("listdata", listMap);
		try {
			productSortTreeNodeService.addOrUpdStandTreeNode(node);
		} catch (DaoException e) {
			if (e.getCause() instanceof SystemException) {
				request.setAttribute("errmsg", e.getCause().getMessage());
			} else {
				request.setAttribute("errmsg", e.getMessage());
			}
			request.setAttribute("formdata", node); // 表单数据
			throw e;
		}
		return node;
	}

	@At("/editNode")
	@Ok("jsp:/sortTree/addNode")
	public void toEditPage(@Param("nodeId") int nodeId, HttpServletRequest request, HttpSession session) {

		// 取得用户数据，放入表单中
		ProductSortTreeNode node = productSortTreeNodeService.getStandTreeNode(nodeId);
		request.setAttribute("formdata", node);

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("pid", createTreeItems(node.getTreeId()));
		session.setAttribute("listdata", itemMap);
	}

	@At("/deleteNode")
	@Ok(">>:/sortTree/listNode.nut?treeId=${obj.treeId}")
	public ProductSortTreeNode delete(int nodeId) {
		ProductSortTreeNode node = productSortTreeNodeService.getStandTreeNode(nodeId);
		productSortTreeNodeService.deleteStandTreeNode(nodeId);
		return node;
	}

	@At("/editTreeNodeFast")
	@Ok("jsp:/sortTree/editTreeNodeFast")
	public void toEditTreePage(@Param("treeId") int treeId, HttpServletRequest request) {

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("treeId", "" + treeId);
		request.setAttribute("formdata", itemMap);
	}

	@At("/getSubNode")
	@Ok("raw:html")
	public String getSubNode(@Param("..") ProductSortTreeNode node, HttpServletRequest request) {

		return productSortTreeNodeService.getSubNodeData(node);
	}

	private String createTreeItems(int treeId) {
		StringBuilder builder = new StringBuilder();
		builder.append("<option value='0'>请选择..</option>");
		builder.append(productSortTreeNodeService.getTreePulldownList(treeId));
		return builder.toString();
	}

	@At("/linkStand")
	@Ok("jsp:/sortTree/linkStand")
	public QueryResult linkStand(@Param("p") int pageNumber, @Param("nodeId") int nodeId,
			@Param("findType") String findType, @Param("standCode") String standCode,
			@Param("standName") String standName, @Param("isLink") String isLink, HttpServletRequest request) {

		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("nodeId", "" + nodeId);
		itemMap.put("findType", findType);
		itemMap.put("standCode", standCode);
		itemMap.put("standName", standName);
		itemMap.put("isLink", isLink);
		request.setAttribute("formdata", itemMap);

		// 查询数据
		return productSortTreeNodeService.getStandLinkList(pageNumber, findType, standCode, standName, nodeId, isLink);
	}

	@At("/doLink")
	@Ok("raw:html")
	public String doLink(@Param("standId") int standId, @Param("nodeId") int nodeId, @Param("isLink") String isLink,
			HttpServletRequest request) {

		//		if (isLink.equals("Y")) {
		//			ProductSortTreeNode linkNode = new ProductSortTreeNode();
		//			linkNode.setStandardId(standId);
		//			linkNode.setNodeId(nodeId);
		//			standardWithTreeNodeService.addOrUpdStandWithTreeNode(linkNode);
		//		} else if (isLink.equals("N")) {
		//			standardWithTreeNodeService
		//					.deleteStandWithTreeNode(standId, nodeId);
		//		}

		return "OK";
	}
}
